package com.etc.service.impl;

import com.etc.dao.UserDao;
import com.etc.entity.User;
import com.etc.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2021-10-18 21:07:18
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;




    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String userId) {
        return this.userDao.deleteById(userId) > 0;
    }

    @Override
    public boolean regist(User user) {
        Integer n = userDao.addUser(user);
        if (n>0){
            return true;
        }
        return false;
    }
//登录
    @Override
    public User login(String userName, String userPassword) {
        User user = userDao.queryUserBynameAndPwd(userName, userPassword);

        return user;
    }
//查盐
    @Override
    public String getUsersaltByUserName(String userName) {
        String salt = userDao.queryUserSaltByUserName(userName);

        return salt;
    }



    @Override
    public List<User> getQueryAllUsers() {

        return userDao.queryAllUser();
    }

    @Override
    public PageInfo<User> queryAllUser(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> list = userDao.queryAllUser();
        PageInfo<User> pageIndo = new PageInfo<>(list);
        return pageIndo;
    }

    /**
     * 通过ID查询用户
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(String userId) {

        return userDao.queryUserById(userId);
    }
     //通过Id删除用户
    @Override
    public boolean deleteUserById(String userId) {
        Integer n = userDao.deleteUserById(userId);

        if (n>0){
            return true;
        }
        return false;

    }

    @Override
    public boolean upUserById(User user) {
        Integer n = userDao.upUserById(user);
        System.out.println(n);
        if (n>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean upUserPriceById(String userId,String price) {
        return userDao.upUserPriceById(userId,price)>0;
    }

    @Override
    public boolean setLevelTwo(String userId) {
        return userDao.setLevelTwo(userId)>0;
    }
}
